const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

const Category = sequelize.define('Category', {
  id: { 
    type: DataTypes.INTEGER.UNSIGNED, 
    primaryKey: true, 
    autoIncrement: true 
  },
  parentId: { 
    type: DataTypes.INTEGER.UNSIGNED, 
    allowNull: false, 
    defaultValue: 0,
    field: 'parent_id' 
    // 父级id，0=一级
  },
  name: { 
    type: DataTypes.STRING(50), 
    allowNull: false, 
    field: 'name' 
    // 分类名称
  },
  sortOrder: { 
    type: DataTypes.INTEGER, 
    allowNull: false, 
    defaultValue: 0,
    field: 'sort_order' 
    // 顺序号，小在前
  }
}, {
  tableName: 'categories',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at'
});

// 添加实例方法以确保字段正确序列化
Category.prototype.toJSON = function() {
  const values = Object.assign({}, this.get());
  // 移除时间字段
  delete values.created_at;
  delete values.updated_at;
  return values;
};

module.exports = Category;